[实操教程] Dell R730 EXSI 虚拟机 Centos7 直通P40 及跨云组网加入k3s

[实操教程] Dell R730 EXSI 虚拟机 Centos7 直通P40 及跨云组网加入k3s 集群

本教程详细介绍了在 Dell R730 上配置 ESXI 虚拟机和直通 P40 显卡的步骤,以及在 CentOS7 上加入 k3s 集群的操作。主要包括虚拟机安装、系统初始化设置、WireGuard 组网(可选)、k3s agent 安装、显卡驱动安装、Docker 安装与配置 NVIDIA Runtime、以及调整磁盘分区等步骤。


一 、 虚拟机配置

新建虚拟机

分配需要的CPU 内存 磁盘, CD 驱动器选择ISO 文件,需提前下载iso镜像

内存 RAM 和预留内存大小需要一致

虚拟机选项设置 EFI 引导

显卡添加

【虚拟硬件】>【添加其他设备】 >【PCI设备】 选择P40 显卡

【虚拟机选项】> 【高级】 > 【配置参数】

必须配置以下内容才能开启电源

1
2
3
4
5
pciPassthru.use64bitMMIO=TRUE

pciPassthru.64bitMMIOSizeGB=64

hypervisor.cpuid.v0= FALSE

Centos 配置

系统安装配置

打开电源 ,启动后,选择控制台 新窗口打开控制台,完成系统安装及配置

进入系统安装引导页,按提示一步一步安装

  • 注意: 分区配置步骤需要调整默认分区大小,否则默认分区基本都会不够用,需要重新调整根目录分区大小

软件选择,勾选开发工具

设置ROOT 密码

等待安装完成, 点击重启, 等待系统启动后输入用户名密码 进入系统

系统初始化设置

此时系统无法访问网络,同时我们也无法通过ssh 访问服务器,需要继续使用vm 控制台完成以下初始化配置

  • 设置静态IP

  • 设置DNS服务器地址

  • 设置主机名

  • yum换源

    执行 ifconfig 命令,查看网卡名称

  • 图中网卡名为:ens224

编辑网卡对应配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens224 ,按i键进入编辑。编辑结束后Esc退出编辑,输入 :wq 保存退出

修改以下配置

  • 注意KEY 全大写

    1
    2
    3
    4
    5
    6
    7
    BOOTPROTO="static" # 使用静态IP地址,默认为dhcp 由DHCP 服务器动态分配
    IPADDR="192.168.3.201" # 静态IP地址
    NETMASK="255.255.255.0" # 子网掩码
    GATEWAY="192.168.3.1" # 网关地址
    DNS1="114.114.114.114" # DNS服务器

    ONBOOT=yes #设置网卡启动方式开机自启

    执行重启网络命令 , 重启后确认网络正常

    1
    2
    3
    systemctl restart network
    curl baidu.com
    ifconfig

    此时 ssh 连接也可正常使用, 可以关闭vm网页控制台,使用ssh 终端连接

  • 此时防火墙未关闭,只能内网连接

    执行hostnameset 命令修改主机名

    1
    hostnamectl set-hostname vm-centos-super-201

    yum 换源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # 备份配置
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    # 下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
    # 配置epel源
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo
    # 清理缓存
    yum clean all
    # 生成缓存
    yum makecache
    # 更新软件包
    yum update

    等待升级完成后,关闭防火墙 [按需,此处外层有路由器NAT 控制端口开放,因此关闭虚拟机防火墙]

    1
    2
    systemctl stop firewalld.service
    systemctl disable firewalld.service

k3s GPU 节点配置

WireGuard 组网 [可选]

关于如何安装WireGuard 及完成的配置生成及使用教程后续将单独提供详细教程, 此处仅仅简单介绍主要流程

安装wireguard

1
2
3
yum install epel-release elrepo-release
yum install yum-plugin-elrepo
yum install kmod-wireguard wireguard-tools

通过 wireguard.debug.icu 生成组网配置内容, 添加到 /etc/wireguard/ 目录下,执行以下命令快速创建网卡(假设生成的配置文件是wg1)

1
wg-quick up wg1

使用curl 或 telnet 测试网络是否畅通 , 确认无误后,设置开启自启

k3s Agent 安装

此处笔者已有一个k3s集群,因此只安装agent 并添加到集群

因为我们已经基于WireGuard 组件好了虚拟局域网,集群的所有通信都是通过虚拟网络的,因此node ip ,node external ip 等都设置为在虚拟网络内的内网IP, 无需配置公网ip , —flannel-iface 设置为前文的配置的网卡名 wg1

1
2
3
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh |  \ 
INSTALL_K3S_MIRROR=cn K3S_URL=https://10.201.1.1:6443 K3S_TOKEN=YOUR_TOKEN sh \
-s - --node-external-ip 10.201.1.2 --node-ip 10.201.1.2 --flannel-iface wg1

等待安装完成,即可在k3s 集群看到节点

安装后,通过 route -n 查看各k3s 节点pod ip 路由是否正常, 正常情况,出虚拟组网的ip 路由外,还有10.42 开头的pod ip 路由, 如原有部分节点可能没有到新节点的路由,需要执行 systemctl restart k3s-agent 重启其他节点的k3s-agent (主节点可执行 systemctl restart k3s

显卡驱动安装

下载显卡驱动

1
2
wget https://us.download.nvidia.cn/tesla/525.147.05/NVIDIA-Linux-x86_64-525.147.05.run
chmod 765 NVIDIA-Linux-x86_64-525.147.05.run

查看内核参数

1
2
uname -r 
ls /usr/src/kernels/

确认显卡正常识别

1
2
lspci | grep NVIDIA
# 0b:00.0 3D controller: NVIDIA Corporation GP102GL [Tesla P40] (rev a1)

执行安装命令

1
./NVIDIA-Linux-x86_64-525.147.05.run --kernel-source-path=/usr/src/kernels/$(uname -r) -k $(uname -r)

首次安装可能会遇到报错:在安装 NVIDIA 驱动程序之前,必须禁用 Nouveau 驱动程序,以避免冲突。

1
2
3
ERROR: The Nouveau kernel driver is currently in use by your system.  This driver is incompatible with the NVIDIA driver,  
and must be disabled before proceeding. Please consult the NVIDIA driver README and your Linux distribution's
documentation for details on how to correctly disable the Nouveau kernel driver.

确认后,会有以下提示, 选择Yes, 让 nvidia-installer 尝试创建 modprobe 文件

1
2
For some distributions, Nouveau can be disabled by adding a file in the modprobe configuration directory.  Would you like  
nvidia-installer to attempt to create this modprobe file for you?

然后出现如下提示:

1
2
3
4
5
One or more modprobe configuration files to disable Nouveau have been written.  For some distributions, this may be        
sufficient to disable Nouveau; other distributions may require modification of the initial ramdisk. Please reboot your
system and attempt NVIDIA driver installation again. Note if you later wish to re-enable Nouveau, you will need to delete
these files: /usr/lib/modprobe.d/nvidia-installer-disable-nouveau.conf,
/etc/modprobe.d/nvidia-installer-disable-nouveau.conf

查看生成的配置文件:

1
2
3
4
cat /etc/modprobe.d/nvidia-installer-disable-nouveau.conf
# generated by nvidia-installer
blacklist nouveau
options nouveau modeset=0

对于Centos7 , nvidia-installer 创建的 modprobe 文件是可以正常禁用Nouveau驱动的,我们只需要重新生成initramfs镜像并重启系统即可

1
2
3
4
5
# 备份当前的 initramfs 镜像文件
mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bakmv
# 重新生成 initramfs 镜像文件
dracut -v /boot/initramfs-$(uname -r).img $(uname -r)
reboot

确认Nouveau 被正常禁用

1
2
3
4
5
6
7
8
9
10
# 禁用前,禁用成功将没有任何输出
lsmod | grep nouveau
nouveau 1940454 0
video 24538 1 nouveau
mxm_wmi 13021 1 nouveau
wmi 21636 2 mxm_wmi,nouveau
i2c_algo_bit 13413 1 nouveau
drm_kms_helper 186531 2 nouveau,vmwgfx
ttm 100769 2 nouveau,vmwgfx
drm 468454 5 ttm,drm_kms_helper,nouveau,vmwgfx

再次重启后,确认Nouveau被禁用后,重新执行驱动安装命令

1
./NVIDIA-Linux-x86_64-525.147.05.run --kernel-source-path=/usr/src/kernels/$(uname -r) -k $(uname -r)

忽略下述提示,后续一路确认即可

执行nvidia-smi 确认驱动安装成功

Nvidia 容器运行时配置

安装Docker ,配置Nvidia Runtime , 并修改k3s agent 的容器运行时为Nvidia docker runtime

设置Docker yum 源, 查看可用版本

1
2
3
4
# 阿里国内镜像
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 查看可用版本
yum list docker-ce --showduplicates | sort -r

选择一个版本并安装

1
yum -y install docker-ce-25.0.3-1.el7

安装NVIDIA 容器工具包(nvidia-container-toolkit)

1
2
3
4
5
6
7
# 配置yum 仓库
curl -s -L https://nvidia.github.io/libnvidia-container/stable/rpm/nvidia-container-toolkit.repo | \
sudo tee /etc/yum.repos.d/nvidia-container-toolkit.repo
# (可选)将存储库配置为使用实验性包
sudo yum-config-manager --enable nvidia-container-toolkit-experimental
# 安装NVIDIA 容器工具包
sudo yum install -y nvidia-container-toolkit

配置docker 国内源

1
vim /etc/docker/daemon.json

添加如下内容(也可使用其他公共源,或自建代理)

1
2
3
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}

启动docker 并设置开机自启

1
2
3
systemctl daemon-reload
systemctl start docker
systemctl enable docker

验证Docker 中可正常使用nvidia 显卡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
docker run  --rm --gpus all  nvidia/cuda:11.0.3-devel-ubuntu20.04 nvidia-smi

#输出结果如下:

==========
== CUDA ==
==========

CUDA Version 11.0.3

Container image Copyright (c) 2016-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

This container image and its contents are governed by the NVIDIA Deep Learning Container License.
By pulling and using the container, you accept the terms and conditions of this license:
https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license

A copy of this license is made available in this container at /NGC-DL-CONTAINER-LICENSE for your convenience.

Sat Feb 24 06:55:23 2024
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.147.05 Driver Version: 525.147.05 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla P40 Off | 00000000:0B:00.0 Off | 0 |
| N/A 78C P0 50W / 250W | 0MiB / 23040MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+

接下来为k3s配置NVIDIA 容器运行时支持,因为k3s在启动时,会自动检测NVIDIA容器运行时是否存在,因此我们只需要执行以下命令重启即可

1
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | sh -

完成后确认nvidia 运行时配置正常

1
2
3
4
5
grep nvidia /var/lib/rancher/k3s/agent/etc/containerd/config.toml
# 执行结果
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes."nvidia"]
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes."nvidia".options]
BinaryName = "/usr/bin/nvidia-container-runtime"

安装

在k3s 上启动GPU pod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
kubectl logs -f nbody-gpu-benchmark
# 输出结果如下
Run "nbody -benchmark [-numbodies=<numBodies>]" to measure performance.
-fullscreen (run n-body simulation in fullscreen mode)
-fp64 (use double precision floating point values for simulation)
-hostmem (stores simulation data in host memory)
-benchmark (run benchmark to measure performance)
-numbodies=<N> (number of bodies (>= 1) to run in simulation)
-device=<d> (where d=0,1,2.... for the CUDA device to use)
-numdevices=<i> (where i=(number of CUDA devices > 0) to use for simulation)
-compare (compares simulation results running once on the default GPU and once on the CPU)
-cpu (run n-body simulation on the CPU)
-tipsy=<file.bin> (load a tipsy model file for simulation)

NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.

> Windowed mode
> Simulation data stored in video memory
> Single precision floating point simulation
> 1 Devices used for simulation
GPU Device 0: "Pascal" with compute capability 6.1

> Compute 6.1 CUDA device: [Tesla P40]
30720 bodies, total time for 10 iterations: 28.389 ms
= 332.420 billion interactions per second
= 6648.391 single-precision GFLOP/s at 20 flops per interaction

本来到这里,核心操作均已完成,但是由于安装Centos时,没有调整默认分区配置,导致根目录只有50G ,下载大模型镜像时因为磁盘空间不足退出了,因此还需要调整下磁盘分区

如果/home 分区下有文件,先备份所有文件 ,然后执行以下命令,对root分区进行扩容

1
2
3
4
5
6
7
8
# 安装fuser命令
yum install -y psmisc
# 要先终止所有使用/home文件系统的进程,这里要注意不要在/home目录下执行下面的操作:
sudo fuser -km /home
# 取消挂载
sudo umount /home
# 移除分区
sudo lvremove /dev/mapper/centos-home

扩大根目录所在逻辑卷大小, 这里全部添加到根分区, 然后执行xfs_growfs 命令拓展文件系统

1
2
sudo lvextend -l +100%FREE /dev/mapper/centos-root
sudo xfs_growfs /dev/mapper/centos-root

再次查看目录磁盘空间大小

1
df -h

大功告成!

END

参考文档